#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpx_supt.h"
#include "MOD1M.h"
#include <dos.h>
#include <dir.h>
#include <time.h>

/* static char array to store the current directory */
static char curr_path[MAX_PATH_SIZE];

/* Entry of ffblk  */
static struct ffblk file_block;

/* */
static int num_entries;



/*Version information*/
float vers = 1.5;
int modMonth = 10;
int modDay = 03;
int modYear = 2008;


/*global variables for readcmd (), processcmd()*/
static int x=1; /*x is our int length of command string, to be used later, y is used to keep track of number of paramaters*/
static char *scmd = NULL;
char *par1, *par2, *par3;

int main(void)
{
      sys_init(MODULE_R2);

      date("get","");
      welcome();
/*dded: use an endless cycle to read, process commands.
//added:  match the input commands with our commands libray and
//added:  decide which functions should be loaded.*/

      queue_init();

      while (1)
     {
        readcmd();
	processcmd();


       if (!strcmp(par1,"help"))
       {
        printf("Load help function.\n");
        help(par2);
        }
       else if ( !strcmp(par1,"version"))
            {
              printf("Load version function.\n");
              version();
            }
       else if ( !strcmp(par1,"date"))
	     {
	      printf("Load date function.\n");
	      date(par2,par3);
	     }
       else if ( !strcmp(par1,"display") )
	       {
	      printf("Load display function.\n");
	      disp(par2);

	      }
       else if ( !strcmp(par1,"exit") )
               {
	      printf("Load exit function.\n");
              mpx_exit();

	      }
       else if(!strcmp(par1,"show")){
	      printf("Load show function.\n");

	      show(par2);

	      }
       else if(!strcmp(par1,"insert")){
	      printf("Load insert PCB function.\n");
	      insertpcb(par2, par3);
	      }
       else if(!strcmp(par1, "remove")){
	      printf("Load delete PCB function.\n");
	      removepcb(par2, par3);
	      }
       else if(!strcmp(par1, "suspend")){
	      printf("Load suspend function.\n");
	      /*function call goes here*/
	      }
       else if(!strcmp(par1, "resume")){
	      printf("Load resume function.\n");
	      /*function call goes here*/
	      }
       else if(!strcmp(par1, "setpriority")){
	      printf("Load set priority function.\n");
	      /*function call goes here*/
	      }

	   else
	     {
	      printf("Unrecognized command.  Try again.\n");
	     }
      }

}


void welcome ()
{
	printf ("Welcome to MPX!\n");
	printf("The following commands can be input at the command prompt.\n");

	printf("-  date <set/get>\n");
	printf("-  display <directory>\n");
	printf("-  version\n");
	printf("-  help <function>\n");
	printf("-  exit\n");
	printf("-  insert <string> <queue name> <priority>\n");
	printf("-  remove <string> <queue name>\n");
	printf("-  show <queue name>");
}


void readcmd()
{
	scmd=sys_alloc_mem(80*sizeof(scmd));
	printf("\n\nMPX>");
	gets(scmd);
}

/* The process function takes the string scmd, the command given by the user
// and passes it to the appropriate function with the proper paramaters*/

void processcmd()
{       int y=0;
	char *p;
	p = sys_alloc_mem(10*sizeof(char));
	par1=sys_alloc_mem(15*sizeof(char));
	par2=sys_alloc_mem(15*sizeof(char));
	par3=sys_alloc_mem(15*sizeof(char));

	p = strtok(scmd," ");

	strcpy(par1, p);
	p = strtok(NULL, " ,");
	while (p != NULL)
	{

		if (y==0){
		strcpy(par2, p);
		 }
		else if (y==1){
		 strcpy(par3, p);
		 }
		y++;
		p = strtok(NULL, " ,");
	}

	printf("%s : %s : %s\n", par1, par2, par3);
}


void mpx_exit()
{

        char ex;
        printf("ARE YOU SURE YOU WANT TO EXIT? ");
        scanf("%c", &ex);

	/*printf("the input is %c\n",ex);*/
        if(ex == 'y' || ex == 'Y')
        {
                printf("Now exiting. . . \n");
                exit(1);
        }
        else if(ex == 'n' || ex == 'N')
        {
                return;
        }
        else
        {
                printf("Invalid command.\n");
                return;
        }
}


void version()
{
    printf("Version Number: ");
    printf("%f\n",vers);
    printf("Last modified: ");
    printf("%d / %d / %d\n", modMonth, modDay, modYear);
}



void disp(char* directory)
{
  char namebuf[60];
  long filesize;

  if(directory==NULL){
     printf("Please specify a directory to search.");
     return;
     }

if (sys_open_dir (directory))
   printf("Can't open the file.\n");

while(!sys_get_entry ( namebuf, 60, &filesize))
{
printf ("%40s\t", namebuf);
printf ("%10ld\n", filesize);
}

if (sys_close_dir())
 printf("Can't close the directory.\n");

}


void help(char* func){


FILE *file;
char c;
char* fn;


fn = (char*)(sys_alloc_mem(25*sizeof(fn)));
strcat(fn,"HELP\\");

printf("Entering help...\n \n");

if(func == NULL)
{
   file=fopen("HELP\\COMMAND.TXT","r");
   if(file==NULL){
      printf("Cannot open command list.  Sorry :( \n \n");
      return;
   }
   else{
      while(!feof(file))
      {
       c = fgetc(file);
       fputc(c,stdout);
      }

      }

      printf("\n \n");

}

else
{
   strcat(fn,func);
   strcat(fn,".HLP");

   file=fopen(fn,"r");
   if(file==NULL)
    {
       printf("Cannot open specified function definition.  Sorry :( \n \n");
       return;
   }
   else
    {
      while(!feof(file))
      {
       c = fgetc(file);
       fputc(c,stdout);
      }

    }
      printf("\n \n");

}
fclose(file);
} /*end help*/


void date(char opt[6], char dte[20]){
   char *p;
   int mon, day, yr;
   int i=0, temp;
   date_rec aDate;

   if(strlen(opt)==3){
     sys_get_date(&aDate);

     mon=aDate.month;
     day=aDate.day;
     yr=aDate.year;

     if(strcmp("set",opt)==0){
      p=strtok(dte,"-");
      while(p!=NULL){
        temp=atoi(p);
        if(i==0){
          if(temp>=1 && temp<=12)
	    aDate.month=temp;
          else{
	    printf("\nNOT A VALID DATE! Invalid month. . . \n");
            return;
          }
        }
        if(i==1){
	  if((aDate.month==9 || aDate.month==4 || aDate.month==6 || aDate.month==11) && temp<=30){
           aDate.day=temp;

           }
          else if(aDate.month==2 && temp<=29){
           aDate.day=temp;

          }
          else if((aDate.month==1 || aDate.month==3 ||aDate.month==5 || aDate.month==7 || aDate.month==8 || aDate.month==10 || aDate.month==12) && temp<=31){
           aDate.day=temp;

          }
          else{
	   printf("\nNOT A VALID DATE!  Invalid day.\n");
           aDate.month=mon;
           aDate.day=day;
           aDate.year=yr;
           return;
           }
        }
        if(i==2){
          if(aDate.month==2 && aDate.day==29){
            if(temp%4==0){
              aDate.year=temp;
              printf("Date successfully set!");
              return;
	      }
	    else{
	      printf("\nNOT A VALID DATE!  Not a leap year.");
	      aDate.month=mon;
	      aDate.day=day;
	      aDate.year=yr;
	      return;
	      }
          }
	  else if(temp > 0 && temp < 9999){
            aDate.year=temp;
            printf("Date successfully set!");
	  }
	  else{
	      printf("\nNOT A VALID DATE!  Invalid year (must be a year between 0 and 9999).");
	      aDate.month=mon;
	      aDate.day=day;
	      aDate.year=yr;
	      return;
	      }
        }
        i++;
        p=strtok(NULL, "-");
      }

      sys_set_date(&aDate);

      }

      else if(strcmp("get",opt)==0){
      sys_get_date(&aDate);

      switch (aDate.month){
         case 1:
           printf("January %d, %u \n", aDate.day, aDate.year);
           break;
         case 2:
           printf("February %d, %d \n", aDate.day, aDate.year);
           break;
         case 3:
           printf("March %d, %d \n", aDate.day, aDate.year);
           break;
         case 4:
           printf("April %d, %d \n", aDate.day, aDate.year);
           break;
         case 5:
           printf("May %d, %d \n", aDate.day, aDate.year);
           break;
         case 6:
           printf("June %d, %d \n", aDate.day, aDate.year);
           break;
         case 7:
           printf("July %d, %d \n", aDate.day, aDate.year);
           break;
         case 8:
           printf("August %d, %d \n", aDate.day, aDate.year);
           break;
         case 9:
           printf("September %d, %d \n", aDate.day, aDate.year);
           break;
         case 10:
           printf("October %d, %d \n", aDate.day, aDate.year);
           break;
         case 11:
           printf("November %d, %d \n", aDate.day, aDate.year);
           break;
         case 12:
           printf("December %d, %d \n", aDate.day, aDate.year);
           break;
         case NULL:
           printf("Date has not yet been set.  Please set today's date.\n");
           break;
         default:
           printf("Invalid date.  Something went wrong here. . .\n");
           printf("Does this help? Month: %d, Day: %d, Year: %d\n", aDate.month, aDate.day, aDate.year);
           break;
        }
      }

      else{
         printf("Not a recognized command. . .");
      }

      }
      else
        printf("Not a recognized command. . .");
}